package c_lockproducer;
/*
    消费者线程
 */
public class Consumer extends Thread{
    public Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        //获取锁
        Resource.lock.lock();
        //flag是false,消费完成,等待生产
        while (!Resource.flag){
            //当前线程进入等待
            try {
                Resource.cust.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("消费第"+Resource.count);
        //标记为消费完成
        Resource.flag = false;
        //唤醒另一个线程
        Resource.pro.signal();
        //释放锁
        Resource.lock.unlock();
    }

}
